use crate::runtime::error::MagicBoxError;
use crate::runtime::response::{Code, MagicBoxResponse};
use crate::runtime::result::MagicBoxResult;

#[tauri::command(rename_all = "snake_case")]
pub(crate) async fn invoke_get_local_ips() -> MagicBoxResult<MagicBoxResponse<Vec<String>>> {
    let mut local_ips: Vec<String> = Vec::new();

    let ips = local_ip_address::list_afinet_netifas();
    if ips.is_err() {
        let err = ips.err().unwrap().to_string();
        log::error!("获取本地IP地址失败: {}", err.as_str());

        return Err(MagicBoxError::new(Code::UndefinedError, format!("获取本地IP地址失败: {}", err.as_str()).as_str()));
    }

    let ips = ips.unwrap();
    for (_, ip) in ips.iter() {
        local_ips.push(ip.to_string());
    }

    Ok(MagicBoxResponse::new_success(local_ips))
}